package org.tinygroup.uiml.executor;

import org.tinygroup.uiml.UiGenerator;
import org.tinygroup.uiml.UimlEngine;
import org.tinygroup.uiml.config.Component;
import org.tinygroup.uiml.config.Layout;
import org.tinygroup.uiml.engine.UimlEngineDefault;
import org.tinygroup.uiml.impl.ConsoleGenerator;
import org.tinygroup.uiml.util.XmlParseSerializableUtil;
import org.tinygroup.vfs.FileObject;
import org.tinygroup.vfs.VFS;

public final class ConsoleExecutor {

	/**
	 * 根据布局文件(*.uiml)模拟代码的生成
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		if (args.length == 0) {
            System.out.println("Usage:\n\tConsoleExecutor uimlFile");
            return;
        }
		
		//解析参数
		String uimlFile = null;     //UIML文件相对路径
		String uimlAbsFile = null;  //UIML文件绝对路径
        if (args.length >= 1) {
        	uimlFile = args[0];
        }
        if(args.length >= 2) {
           uimlAbsFile = args[1];
        }
        if(uimlAbsFile==null){
        	uimlAbsFile = uimlFile;
        }
        
        
        //初始化UIML引擎和代码生成器
        UiGenerator generator = new ConsoleGenerator();
        UimlEngine engine = UimlEngineDefault.getInstance();
        
        //将布局文件反序列化为Java对象
        FileObject fileObject = VFS.resolveFile(uimlAbsFile);
        
        //根据布局文件加载UIML的引擎对象
        Component component=  XmlParseSerializableUtil.readUIML(fileObject.getInputStream(), Layout.class.getClassLoader());
        engine.addComponent(component); 
        
        
        //生成代码
        generator.generate(engine);
	}
	

}
